package com.eryos.gwt.mailmodule.client;

import com.eryos.gwt.mailmodule.shared.CaptchaCheck;
import com.eryos.gwt.mailmodule.shared.MailMessage;
import com.eryos.gwt.mailmodule.shared.Person;
import com.eryos.gwt.mailmodule.shared.exception.InvalidCaptchaException;
import com.eryos.gwt.mailmodule.shared.exception.InvalidMailException;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MailPresenter {

    public interface View {
        String getNameFieldValue();
        String getMailFieldValue();
        String getSubjectFieldValue();
        String getMessageFieldValue();

        HasClickHandlers getValidButton();
        String getCaptchaChallengeValue();
        String getCaptchaResponseValue();

        void updateStatus(String _msg);
        void init();
        Widget asWidget();
    };
    
    private View view;
    private MailServiceAsync service;

    public MailPresenter( MailServiceAsync service, View _view){
    	System.out.println("new Presenter");
    	this.service = service;
    	this.view = _view;

    	init();
	}

	public void go(final HasWidgets container) {
    	System.out.println("go");
		container.add(view.asWidget()); 

		getView().init();
	}
    
    /**
     * This is the 'entry point' of the presenter
     */
    public void init() {
    	System.out.println("init Presenter");
    	
        getView().getValidButton().addClickHandler(new ClickHandler(){

            @Override
            public void onClick(ClickEvent event) {
                System.out.println("Click ...");
            	validMailForm();
            }
        });
    }

    /**
     *
     */
    private void validMailForm(){
    	System.out.println("validMailForm");
        getView().updateStatus("Waiting .. check in progress ...");

        String challenge    = getView().getCaptchaChallengeValue();
        String response    = getView().getCaptchaResponseValue();

        CaptchaCheck captcha = new CaptchaCheck(challenge, response);

        MailMessage mail = new MailMessage();
        Person sender = new Person();
        sender.setLastName(getView().getNameFieldValue());
        sender.setMailAddress(getView().getMailFieldValue());
        mail.setSender(sender);
        mail.setSubject(getView().getSubjectFieldValue());
        mail.setContent(getView().getMessageFieldValue());

		getMailService().sendMailWithCaptcha(mail, captcha, new AsyncCallback<Boolean>(){

		    @Override
		    public void onFailure(Throwable caught) {
		        if ( caught instanceof InvalidCaptchaException )
		            getView().updateStatus("Captcha ERROR ... try again ...");
		        else if ( caught instanceof InvalidMailException )
		            getView().updateStatus("Mail adress ERROR ... try again ...");
		        else if ( caught instanceof IllegalArgumentException )
		        	getView().updateStatus("IllegalArgumentException ...");
		        else
		            getView().updateStatus("!!! Unattended Error (1) !!!");
		    }

		    @Override
		    public void onSuccess(Boolean result) {
		        if ( result ){
		            // Captcha OK & Mail Sent                    
		            getView().updateStatus("Captcha OK ... congratulation ...");
		        }else
		            getView().updateStatus("!!! Unattended Error (2) !!!");                
		    }
		});
    }

    private View getView(){
        return view;
    }

    private MailServiceAsync getMailService(){
        return service;
    }
}